package com.itxiuyixiu.security.service;

import com.itxiuyixiu.entity.model.Admin;
import com.itxiuyixiu.security.mapper.AdminMapper;
import com.itxiuyixiu.tools.security.user.UserNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 黄磊
 * @date 2020/5/1
 */
@Service
public class AdminDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Resource
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = adminMapper.findAdminByUsername(username);
        if (admin == null) {
            throw new UserNotFoundException("没有找到用户：" + username);
        }
        return User.withUsername(username).password(admin.getPassword()).authorities(admin.getAuthorities()).build();
    }
}
